﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
namespace com.dz.common
{
    public static class CookieManager
    {
        static readonly string Domain = AppConfig.Domain;

        const string VifCode = "vc";
        const string LoginInfo = "ll";
        const string bookClick = "bookClick";
        const string chapter = "c";
        /// <summary>
        /// 章节阅读cookie
        /// </summary>
        const string read_chapter_cookie = "rc";

     

        /// <summary>
        /// 设置验证码
        /// </summary>
        /// <param name="cookieValue"></param>
        public static void SetVifCode(string cookieValue)
        {
            HttpCookie cookie = new HttpCookie(VifCode);

            if (!string.IsNullOrEmpty(Domain))
                cookie.Domain = Domain;

            cookie.Value = cookieValue;
           
            HttpContext.Current.Response.Cookies.Add(cookie);
        }


        /// <summary>
        /// 获取设置的验证码
        /// </summary>
        /// <returns></returns>
        public static string GetVifCode()
        {

            if (HttpContext.Current.Request.Cookies[VifCode] != null)
                return HttpContext.Current.Request.Cookies[VifCode].Value;

            return string.Empty;
        }

        /// <summary>
        /// 设置登陆信息
        /// </summary>
        /// <param name="cookieValue"></param>
        public static void SetLoginUserInfo(string cookieValue)
        {
            HttpCookie cookie = new HttpCookie(LoginInfo);

            if (!string.IsNullOrEmpty(Domain))
                cookie.Domain = Domain;

            cookie.Value = cookieValue;


            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 设置书籍点击次数
        /// </summary>
        /// <param name="i_book_id"></param>
        public static void SetBookReadCount(long i_book_id)
        {
            HttpCookie cookie = new HttpCookie(bookClick);

            if (!string.IsNullOrEmpty(Domain))
                cookie.Domain = Domain;

            cookie.Value = i_book_id.ToString();

            cookie.Expires = DateTime.Now.AddMinutes(3);


            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 判断是否是要加书籍点击 true 是需要更新点击 false不需要
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <returns></returns>
        public static bool GetBookReadCount(long i_book_id)
        {
            if (HttpContext.Current.Request.Cookies[bookClick] != null)
            {
                string t_book_click_cookie = HttpContext.Current.Request.Cookies[bookClick].Value;
                if (t_book_click_cookie.IndexOf(i_book_id.ToString()) > -1)
                    return false;
            }
            SetBookReadCount(i_book_id);
            return true;
        }


        /// <summary>
        /// 获取登陆用户的登陆名
        /// </summary>
        /// <returns></returns>
        public static string GetLoginUser()
        {
            if (HttpContext.Current.Request.Cookies[LoginInfo] != null)
                return HttpContext.Current.Request.Cookies[LoginInfo].Value;

            return string.Empty;
        }

        /// <summary>
        /// 设置用户退出登录信息
        /// </summary>
        public static void SetUserLoginOut()
        {
            HttpCookie curCookie = null;

            if (String.IsNullOrEmpty(GetLoginUser()))
            {
                curCookie = new HttpCookie(LoginInfo);
            }
            else
            {
                curCookie = HttpContext.Current.Request.Cookies[LoginInfo];
            }
            if (!string.IsNullOrEmpty(AppConfig.Domain))
                curCookie.Domain = AppConfig.Domain;
            curCookie.Expires = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.Cookies.Add(curCookie);

        }

        /// <summary>
        /// 设置章节阅读cookie 以便继续上次阅读
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_chapter_id"></param>
        public static void SetChapterReadCookie(long i_book_id,string i_book_name, long i_chapter_id,string i_chapter_name)
        {
            string t_cur_read = GetChapterReadCookie();
            StringBuilder sb = new StringBuilder();

            if (!string.IsNullOrEmpty(t_cur_read))
            {
                string[] t_arr = t_cur_read.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);
                string[] t_book_arr=null;
                long t_book_id;

                for (var index = (t_arr.Length-1); index >-1; index--)
                {
                    t_book_arr=t_arr[index].ToString().Split(new char[]{'#'},StringSplitOptions.RemoveEmptyEntries);

                    if (!long.TryParse(t_book_arr[0], out t_book_id))
                        t_book_id = 0;
                    
                    if (t_book_id != i_book_id)
                    {
                        sb.AppendFormat("{0}#{1}#{2}#{3}", t_book_id, t_book_arr[1],t_book_arr[2].ToString(), t_book_arr[3].ToString());
                        sb.Append("$");

                        if (index == t_arr.Length - 3)
                            break;
                    }
                }
            }

            sb.AppendFormat("{0}#{1}#{2}#{3}", i_book_id, i_chapter_id,HttpUtility.UrlEncode( i_book_name),HttpUtility.UrlEncode( i_chapter_name)).Append("$");

            HttpCookie cookie = new HttpCookie(read_chapter_cookie);

            if (!string.IsNullOrEmpty(Domain))
                cookie.Domain = Domain;

            cookie.Value = sb.ToString();
            cookie.Expires = System.DateTime.Now.AddMonths(6);


            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 获取章节阅读cookie 以便继续上次阅读
        /// </summary>
        /// <returns></returns>
        public static string GetChapterReadCookie()
        {
            if (HttpContext.Current.Request.Cookies[read_chapter_cookie] != null)
                return HttpContext.Current.Request.Cookies[read_chapter_cookie].Value;

            return string.Empty;
        }

        /// <summary>
        /// 设置章节阅读页cookie
        /// </summary>
        /// <param name="i_book_id"></param>
        /// <param name="i_chapter_id"></param>
        public static void SetChapterCookie(long i_book_id,long i_chapter_id)
        {
            HttpCookie cookie = new HttpCookie(chapter);

            if (!string.IsNullOrEmpty(Domain))
                cookie.Domain = Domain;

            cookie.Value =SystemUtil.MD5Mac(string.Format("{0},{1},{2}", i_book_id,i_chapter_id,AppConfig.UrlKey));
            cookie.Expires = DateTime.Now.AddMonths(10);


            HttpContext.Current.Response.Cookies.Add(cookie);
        }

        /// <summary>
        /// 获取章节页cookie
        /// </summary>
        /// <returns></returns>
        public static string GetChapterCookie()
        {
            if (HttpContext.Current.Request.Cookies[chapter] != null)
                return HttpContext.Current.Request.Cookies[chapter].Value;

            return string.Empty;
        }

        /// <summary>
        /// 获取当前登录用户的id
        /// </summary>
        /// <returns></returns>
        public static long GetUserId()
        {
            string t_cur_user = GetLoginUser();
            if (string.IsNullOrEmpty(t_cur_user))
                return 0;

            string[] t_arr = t_cur_user.Split(new char[] { '$' }, StringSplitOptions.RemoveEmptyEntries);
            if (t_arr.Length != 2)
                return 0;
            long t_user_id = 0;
            if (!long.TryParse(t_arr[1].ToString(), out t_user_id))
                t_user_id = 0;
            return t_user_id;
        }
    }
}
